package com.zx.mobile.controller;

import java.util.Map;
import java.util.Objects;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.zx.map.service.OrdLogService;
import com.zx.map.service.model.MerchantInfoDetailModel;
import com.zx.map.utils.CommonResponseUtils;
import com.zx.mobile.utils.WebUtils;

@Controller
@RequestMapping("ordLog")
public class OrdLogController {
	
	private static Logger log = Logger.getLogger(OrdLogController.class); 
	
	@Autowired
	private OrdLogService ordLogService;
	
	@RequestMapping("/orderList")
	@ResponseBody
	public Map todayOrderList(@RequestBody Map<String, Object>params, HttpServletRequest request) {
		try {
			MerchantInfoDetailModel merchant = WebUtils.queryCurrentLoginMerchantInfoDetailModel(request);
			String merchantId = merchant.getMerchantId();
			Objects.requireNonNull(merchantId);
			params.put("merchantId", merchantId);
			Map<String, Object> result = ordLogService.getorderList(params);
			return CommonResponseUtils.successResponse(result);
		}catch(Throwable e) {
			e.printStackTrace();
			return CommonResponseUtils.failureResponse(e.getMessage());
		}
	}
	
	@RequestMapping("/history")
	@ResponseBody
	public Map history(HttpServletRequest request) {
		try {
			MerchantInfoDetailModel merchant = WebUtils.queryCurrentLoginMerchantInfoDetailModel(request);
			String merchantId = merchant.getMerchantId();
			Objects.requireNonNull(merchantId);
			Map<String, Object> result = ordLogService.getAnalysisForHistoryMonth(merchantId);
			return CommonResponseUtils.successResponse(result);
		}catch(Throwable e){
			e.printStackTrace();
			return CommonResponseUtils.failureResponse(e.getMessage());
		}
		
	}
	
	@RequestMapping("/homePage")
	@ResponseBody
	public Map homePage(HttpServletRequest request) {
		try {
			MerchantInfoDetailModel merchant = WebUtils.queryCurrentLoginMerchantInfoDetailModel(request);
			String merchantId = merchant.getMerchantId();
			Objects.requireNonNull(merchantId);
			Map<String, Object> result = ordLogService.homePageData(merchantId);
			return CommonResponseUtils.successResponse(result);
		}catch(Throwable e){
			e.printStackTrace();
			return CommonResponseUtils.failureResponse(e.getMessage());
		}
	}
}
